<template>
  <div class="login-container">
    <!-- 登录 -->
    <el-form v-if="form" ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <h1 class="login" />
      <div class="title-container">
        <h3 class="title">
          后 台 登 录 系 统
        </h3>
      </div>

      <el-form-item prop="identifier">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="identifier"
          v-model="loginForm.identifier"
          placeholder="请输入用户名"
          name="identifier"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登 录</el-button>

      <button class="register" @click.prevent="toRegister">
        没有账号？去注册
      </button>

    </el-form>

    <!-- 注册 -->
    <el-form v-else ref="registerForm" :model="registerForm" :rules="loginRules" class="register-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">
          注 册 新 用 户
        </h3>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="registerForm.username"
          placeholder="创建一个用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="email">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="email"
          v-model="registerForm.email"
          placeholder="请输入你的邮箱号"
          name="email"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="registerForm.password"
          :type="passwordType"
          placeholder="请设置密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleRegister"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleRegister">注 册</el-button>

      <button class="register" @click.prevent="toLogin">
        去登录
      </button>

    </el-form>
  </div>
</template>

<script>
import { register } from '@/api/user'

export default {
  name: 'Login',
  data() {
    return {
      // 控制显示隐藏的变量
      form: true,
      // 登陆表单对象
      loginForm: {
        identifier: 'admin',
        password: 'adminadmin'
      },
      // 注册表单对象
      registerForm: {
        username: '',
        email: '',
        password: ''
      },
      // 校验规则
      loginRules: {
        identifier: [
          { required: true, message: '账户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空！', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空！', trigger: 'blur' },
          { pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, message: '邮箱格式不正确！', trigger: 'blur' }
        ],
        username: [{ required: true, message: '用户名不能为空！', trigger: 'blur' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    // 展示密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录按钮事件************************************************************************************登陆注册按钮二次校验可能有误，注册有误也会进行跳转和弹成功消息，需要修改
    handleLogin() {
      this.$refs.loginForm.validate(async(valid) => {
        if (valid) {
          await this.$store.dispatch('user/getToken', this.loginForm)
          this.$router.push('/')
          this.$message({
            message: '登录成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '登录失败',
            type: 'error'
          })
          return false
        }
      })
    },

    // 去注册按钮
    toRegister() {
      this.form = false
    },
    //
    toLogin() {
      this.form = true
    },
    // 注册按钮事件
    async handleRegister() {
      try {
        // 发送请求
        await register(this.registerForm)
        // 跳转到login表单
        this.form = true
        // 并且回填
        const { username, password } = this.registerForm
        this.loginForm.identifier = username
        this.loginForm.password = password
        // 再清空注册表单
        this.registerForm = {
          username: '',
          email: '',
          password: ''
        }
        this.$message({
          message: '注册成功，为您跳转到登陆页面',
          type: 'success'
        })
      } catch (error) {
        this.$message({
          message: '注册失败',
          type: 'error'
        })
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
 background-image: url('https://desk-fd.zol-img.com.cn/t_s1440x900c5/g2/M00/03/07/ChMlWV66EA6IEHSXAB-Xrg0G1mAAAPC_AIIE7QAH5fG463.jpg')  ;background-position: center;

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
     .el-form-item__error {
    color: #fff
  }
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 120px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    left: 15%;
    // background-color: #666;
    .el-button{
      background-color:rgba(255, 255, 255, 0.1);
      border:0;
      height: 50px;
      font-size: 20px;
      font-weight: 700;
    }
  }
   .register-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 120px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    left: 15%;
    .el-button--primary{
      background-color:rgba(255, 255, 255, 0.1);
      border:0;
      height: 50px;
      font-size: 20px;
      font-weight: 700;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .register{
    color:#fff;
    background-color: #5b677700;
    border:none
  }
}
</style>
